﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube : MonoBehaviour
{
    public GameObject[] rewards;
    public GameObject deadEff;
    public bool dead = false;
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Bullet"))
        {
            if (dead)
            {
                return;
            }
            dead = true;
            //1.打到自己的子弹要被销毁.
            //子弹类中设置了打到Cube就销毁自己，所以将Cube的Tage设置为Cube即可

            //2.打到后，随机一个奖励
            int rangeInt = Random.Range(0, 100);
            if (rangeInt <= 60)
            {
                rangeInt = Random.Range(0, rewards.Length);
                Instantiate(rewards[rangeInt], this.transform.position, this.transform.rotation);
            }
           
            //播放奖励特效
            GameObject eff = Instantiate(deadEff, this.transform.position, this.transform.rotation);
            //由于该特效身上挂了音效,需要控制音效和音效音量
            AudioSource audioSource = eff.GetComponent<AudioSource>();
            if (audioSource)
            {
                //根据音乐数据 设置音效大小和是否播放
                //设置音效大小
                audioSource.volume = GameDataMgr.Instance.musicData.audioVolume;
                //音效是否播放
                audioSource.mute = !GameDataMgr.Instance.musicData.audio;
                //避免没有勾选 Play on Awake
                audioSource.Play();
            }
            Destroy(this.gameObject);
        }
    }
}
